package prop;

/// Autor: Daniel Mart�nez Bou
// Cluster 3.2
// �ltima versi�: 14/11/08

import java.util.Date;
import java.io.*;


public final class NoticiaReduida  {
	
	// ############### Variables ###############
	
	private class contparaula{
		String paraula;
		int freq;
		contparaula seg;
		public contparaula(String paraula) {
			this.paraula = paraula;
			this.freq = 1;
			this.seg = null;
			numparaules++;
		}
	}
	             
	private int id;
	private contparaula paraules;
	private String seccio;
	private Date data;
	private String autor;
	private String font;
	private String medi;
	private String idioma;
	private int numparaules;
	
	/*public class tupla{
		String paraula;
		int frequencia;
	}*/
	
	// ############### M�todes ###############
	
	public NoticiaReduida(){
		paraules = null;
		numparaules = 0;
	}
	
	public void setId(int id){
		this.id = id;
	}
	public int getId(){
		return this.id;
	}
	
	public void setParaules(String paraula){
		paraula = paraula.toLowerCase();
		if(this.paraules == null){
			this.paraules = new contparaula(paraula);
		}else{
			contparaula aux = null;
			contparaula principal = this.paraules;
			boolean trobat = false;
			int comp;
			
			while(principal != null && !trobat){
				comp = principal.paraula.compareTo(paraula);
				if(comp == 0){ //IGUAL
					principal.freq++;
					trobat = true;
				}else if(comp < 0){ //MAJOR
					aux = principal;
					principal = principal.seg;
				}else{ //MENOR
					trobat = true;
					contparaula novaparaula = new contparaula(paraula);
					if(aux == null){//PRIMER
						novaparaula.seg = principal;
						paraules = novaparaula;
					}else{//ENMIG
						aux.seg = novaparaula;
						novaparaula.seg = principal;
					}
				} 
			}
			if(!trobat){ // ULTIM
				contparaula novaparaula = new contparaula(paraula);
				aux.seg = novaparaula;
			}
		}
	}
	public tupla[] getParaules() throws Exception{
		tupla[] vectortuples = new tupla[numparaules];
		int i = 0;
		contparaula punter = paraules;
		while(punter != null && i < numparaules){
			vectortuples[i] = new tupla();
			vectortuples[i].frequencia = punter.freq;
			vectortuples[i].paraula = punter.paraula;
			i++;
			punter = punter.seg;
		}
		if(punter != null || i != numparaules)throw new Exception ("Database structure is corrupted (NoticiaReduida)");
		return vectortuples;
	}
	
	public void setSeccio(String seccio){
		this.seccio = seccio;
	}
	public String getSeccio(){
		return this.seccio;
	}
	public void setData(Date data){
		this.data = data;
	}
	public Date getData(){
		return this.data;
	}
	public void setAutor(String autor){
		this.autor = autor;
	}
	public String getAutor(){
		return this.autor;
	}
	public void setFont(String font){
		this.font = font;
	}
	public String getFont(){
		return this.font;
	}
	public void setMedi(String medi){
		this.medi = medi;
	}
	public String getMedi(){
		return this.medi;
	}
	public void setIdioma(String idioma){
		this.idioma = idioma;
	}
	public String getIdioma(){
		return this.idioma;
	}
	public int getNumparaules(){
		return this.numparaules;
	}
	
	// ############### Joc de proves ###############
	
	private static void printMenu(){
		System.out.println("== MENU ==");
		System.out.println("  Consultar id (1)");
		System.out.println("  Consultar autor (2)");
		System.out.println("  Consultar secci� (3)");
		System.out.println("  Consultar data (4)");
		System.out.println("  Consultar font (5)");
		System.out.println("  Consultar medi (6)");
		System.out.println("  Consultar idioma (7)");
		System.out.println("  Consultar totes les paraules (8)");
		System.out.println("  Sortir (0)");
	}
	public static void main (String args[]){
		
		try {
			NoticiaReduida noticia = new NoticiaReduida();
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			String paraula;
			tupla[] noticies;
			int op;
			int nump;
			int i;			
			
			do{
				System.out.println("== AFEGIR NOTICIA ==");
				System.out.println(">Escriu l'id:");
				noticia.setId(Integer.parseInt(in.readLine()));
				System.out.println(">Escriu l'autor:");
				noticia.setAutor(in.readLine());
				System.out.println(">Escriu la secci�:");
				noticia.setSeccio(in.readLine());
				System.out.println(">Escriu la data (AAAA/MM/DD):");
				noticia.setData(new Date(in.readLine()));
				System.out.println(">Escriu la font:");
				noticia.setFont(in.readLine());
				System.out.println(">Escriu el medi:");
				noticia.setMedi(in.readLine());
				System.out.println(">Escriu l'dioma:");
				noticia.setIdioma(in.readLine());
				do{
					System.out.println(">Afegeix una paraula (0 per acabar):");
					paraula=in.readLine();
					if(paraula.compareTo("0")==0){
						break;
					}
					noticia.setParaules(paraula);
				}while(true);
				printMenu();
				do{
					op = Integer.parseInt(in.readLine());
					switch(op){
					case 1:
						System.out.println(noticia.getId());
						break;
					case 2:
						System.out.println(noticia.getAutor());
						break;
					case 3:
						System.out.println(noticia.getSeccio());
						break;
					case 4:
						System.out.println(noticia.getData());
						break;
					case 5:
						System.out.println(noticia.getFont());
						break;
					case 6:
						System.out.println(noticia.getMedi());
						break;
					case 7:
						System.out.println(noticia.getIdioma());
						break;
					case 8:
						noticies = noticia.getParaules();
						nump = noticia.getNumparaules();
						for(i = 0;i < nump;i++){
							System.out.println(noticies[i].paraula+":" + noticies[i].frequencia);
						}
						if(nump == 0){
							System.out.println("No hi ha paraules a la not�cia.");
						}
						break;
					case 0:
						continue;
					default:
						printMenu();
					}
				}while(op != 0);		
				do{
					System.out.println("\n== MENU ==");
					System.out.println("  Afegir nova not�cia (1)");
					System.out.println("  Sortir (0)");
					op = Integer.parseInt(in.readLine());
				}while(op != 1 && op != 0);
			}while(op != 0);
			
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}	
        public class tupla{
		String paraula;
		int frequencia;
	}
}